package com.delu.service.impl;

import com.delu.entity.User;
import com.delu.entity.Wallet;
import com.delu.mapper.UserMapper;
import com.delu.mapper.WalletMapper;
import com.delu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WalletMapper walletMapper;
    @Override
    public User selectByName(String username) {
        User user  = userMapper.selectByUserName(username);
        return user;
    }

    @Override
    public void insertUser(User user) {
        userMapper.insert(user);
        //用户注册后修改钱包表
        Wallet wallet = new Wallet();
        wallet.setUserId(user.getUserId());
        wallet.setBalanceAccount(new BigDecimal(0));
        wallet.setDebt(new BigDecimal(0));
        walletMapper.insert(wallet);
    }

    @Override
    public void updateUserByUserName(String usrname) {
        userMapper.updateUserByUserName(usrname,new Date());
    }

    @Override
    public User selectUserIdByUserName(String usrname) {
        return userMapper.selectByUserName(usrname);
    }

    @Override
    public void updateUserByUserId(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User selectUserIdByUserPhoneNum(String phoneNum) {
        return userMapper.selectUserIdByUserPhoneNum(phoneNum);
    }

    @Override
    public boolean selectPhoneNum(String phoneNum) {
        User user =  userMapper.selectUserIdByUserPhoneNum(phoneNum);
        if (user!=null){
            return true;
        }else {
            return false;
        }

    }
}
